package com.example.demo.controller;


import com.example.demo.constant.Constant;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    //登录
    @RequestMapping("/login")
    public Boolean login(String name,String password,HttpSession session){
        //1.参数校验
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)){
            return false;
        }

        //根据用户名获取用户信息
        UserInfo userInfo= userInfoService.queryUserInfoByName(name);
        if(userInfo==null){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constant.SESSION_USER_KEY,userInfo);
            return true;
        }
        return false;
    }


}
